package com.yuchengtech.simpleServer.jdv;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;

import com.yuchengtech.simpleServer.core.HttpRequest;

public class JdvUtil {
	
	public static final char CHAR_$='$';
	public static final char CHAR_QUTO='{',CHAR_ENDQUTO='}';
	public static void parseJdvMark(JdvFile jdvFile,StringBuilder content)
	{
		jdvFile.clearMarks();
		
		int start=0,end=0;
		boolean markStarted=false;
		boolean inQuto=false;
		char preChar=content.charAt(0);
		for(int i=1;i<content.length();i++)
		{
			char ch=content.charAt(i);
			if(markStarted)
			{
				if(ch=='"')
				{
					inQuto=!inQuto;
				}
				
				if(!inQuto)//如果不是在"里面,则进行字符判断
				{
					if(!checkCharLegal(ch))
						{
						markStarted=false;
						}
					if(CHAR_ENDQUTO==ch)
						{
							end=i;
							if(start+2!=end)
							{
							JdvMark jdvMark=new JdvMark();
							jdvMark.setStart(start);
							jdvMark.setEnd(end);
							String paramPath=content.substring(start+2,end).trim();
							jdvMark.setParamPath(paramPath);
							if(paramPath.length()!=0)
								jdvFile.addJdvMark(jdvMark);
							}
							markStarted=false;
						}
				}
				
			}
			else 
				if(ch==CHAR_QUTO&&preChar==CHAR_$)
			{
				markStarted=true;
				start=i-1;
			}
			
			preChar=ch;
		}
	}
	
	private static char[] ILLLEGAL_CHARS={CHAR_$,CHAR_QUTO,'/','>','<','(',
										')','!','@','#','%','^','&','*','-','_','+','=','\n'};
	
	private static boolean checkCharLegal(char c)
	{
		for(int i=0;i<ILLLEGAL_CHARS.length;i++)
		{
			if(c==ILLLEGAL_CHARS[i])
			return false;
		}
		return true;
	}
	
	
	
	public static String parseJdvContent(HttpRequest request,JdvFile jdvFile,StringBuilder content)
	{
		List<JdvMark> markList=jdvFile.getMarkList();
		int lengthPlus=0;
		HashMap<String,Object> lazyMap=new HashMap<String, Object>();//lazy datas
		
		for (JdvMark jdvMark : markList) {
			String paramPath=jdvMark.getParamPath();
			int start=jdvMark.getStart();
			int end=jdvMark.getEnd();
			if(!checkParamPathLegal(paramPath))
				continue;
			
			
			Object paramObj=getParamValue(request,paramPath,lazyMap);
			String paramValue;
			if(paramObj==null)
				paramValue="";
			else 
			  paramValue=paramObj.toString();
			
			content.replace(start+lengthPlus,end+lengthPlus+1,paramValue);
			lengthPlus=lengthPlus+(paramValue.length()-end+start-1);
		}
		return content.toString();
	}
	
	
	
	private static Object getParamValue(HttpRequest request, String paramPath,
			HashMap<String, Object> lazyMap) {
		String paramValue=request.getParameter(paramPath);
		if(paramValue==null)
		{
			Object obj=request.getAttribute(paramPath);
			if(obj!=null)
				return obj;
			
			String[] parts=paramPath.split("[\\.]");
			 obj=request.getAttribute(parts[0]);
			if(obj==null)
				return null;
			
			if(parts.length==1)
			return obj;
			
			return getObjValue(obj,parts,1);
		}
		return paramValue;
	}

	private static Object getObjValue(Object obj,String[] parts,int i)
	{
		Class<?> fieldClass=obj.getClass();
		 try {
			 Method getMethod=fieldClass.getDeclaredMethod(getGetName(parts[i]));
			 Object subObj=getMethod.invoke(obj);
			 if(subObj==null)
				 return null;
			 i++;
			 if(i==parts.length)
				 return subObj;
			 return getObjValue(subObj, parts, i);
		} catch (Exception e) {
			return null;
		}
	}

	public static String getGetName(String keyName) {
		return "get"+keyName.substring(0,1).toUpperCase()+keyName.substring(1);
	}

	//not finished ,it's just test s
	private static boolean checkParamPathLegal(String paramPath)
	{
		//String trimedPath=paramPath.trim();
		return true;
	}
	
	
	
}
